

import bpy
import idprop

import 全局控制.Node.插座.bl_插座基类 as 插座基类
from 节点.Core import bl_UI配色



#from 全局控制.Node.插座.插座基类 import bl_数组数据插座基类
#from 全局控制.Node.插座.插座基类 import bl_List基类
from 全局控制.Node.节点数据类型 import bl_集合数据类型

from itertools import chain



class C_NodeSocket_物体(插座基类.C_NodeSocket_基类):
	bl_label = '对象'
	
	#m_Value = idprop.types.IDPropertyArray.mro()
	m_Value = bpy.props.CollectionProperty(type=bl_集合数据类型.C_物体组元素)
	def __init__(self):
		super(C_NodeSocket_物体, self).__init__()
		#self.m_Value.append(bpy.context.object)
		#self.m_Value.pop(0)
		self.f_setType('s_Object')

	def draw(self, context, layout, node, text):
		layout.label(text=text+':'+self.name)
		return
		if self.is_output :
			layout.label(text=self.name + ':' + str(len(self.m_Value)) )
			return
		layout.label(text=self.name + ':' + str(len(self.f_getData())) )
		

	def f_getData(self, key=-1):
		data = super(C_NodeSocket_物体, self).f_getData(key)
		#print('C_NodeSocket_物体 = ', key, data)
		if data != None :
			#print('objs data = ', data)
			try:
				data = list(chain(*data))
			except :
				if type(data).__name__ != 'list':
					data = [data]
			return data

		if key >= 0 :
			try:
				num = len(self.m_Value)
				if num > 0 and key < num:
					return self.m_Value[key].value
				else:
					self.m_状态 = -1
					return None
			except :
				return self.m_Value[0].value
		else:
			v = []
			#print('C_NodeSocket_物体 = ', self.m_Value)
			for i in self.m_Value:
				#print('i ==================== ', type(i), i, i.value)
				v.append(i.value)
			
			try:
				v = list(chain(*v))
			except :
				pass
			
			
			return v
		
	def f_setData(self, data):
		self.m_Value.clear()
		
		obj = None
		try:
			obj = []
			for i in data:
				obj.append(i)
		except :
			obj = [data]
		
		for i in obj:
			#if type(i).__name__ != 'Object' :
			#	self.report({'ERROR'}, "物体插座传入的数据不是物体对象")
			item = self.m_Value.add()
			item.value = i
			
		#pass

	def f_addData(self, data, 排除重复=False):
		for obj in data:
			if 排除重复 :
				是否跳过 = False
				for item in self.m_Value :
					if item.value.name == obj.name :
						是否跳过 = True
				if 是否跳过 :
					continue

			item = self.m_Value.add()
			item.value = obj

	def f_pop多Data(self, ids=[]):
		if len(ids) == 1:
			self.m_Value.remove(ids[0])
		else:
			for i in range(ids):
				self.m_Value.remove(i)
		
	def f_popData(self, name):
		index = 0
		for e in self.m_Value:
			if e.m_Obj.name == name :
				self.f_pop多Data([index])
				return
			index += 1
	
	def f_clear(self):
		self.m_Value.clear()





'''class C_插座_物体2D(bl_数组数据插座基类.C_2D数组数据插座基类):
	bl_label = '物体 vec2'
	
	m_Value = bpy.props.CollectionProperty(type=bl_集合数据类型.C_2D_物体组元素)
	def __init__(self):
		super(C_插座_物体2D, self).__init__()
		'''
	
















class C_Socket_标量值模板(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_标量值模板, self).__init__()
		
	def draw_color(self, context, node):
		return (0.0, 0.0, 0.0, 0.5)
		
class C_Socket_Vector模板(插座基类.C_Socket_基类):
	def __init__(self):
		super(C_Socket_Vector模板, self).__init__()
		
	def draw_color(self, context, node):
		return (0.0, 1.0, 1.0, 1.0)











class C_NODE_AddMenu_添加插座按钮(bpy.types.Operator):
	bl_idname = 'node.add_socket_button'
	bl_label = '添加插座'
	bl_options = {'REGISTER', 'UNDO'}
	m_TreeName = bpy.props.StringProperty()
	m_NodeName = bpy.props.StringProperty()
	m_插座类型 = bpy.props.StringProperty()
	m_插座维度 = bpy.props.StringProperty()
	def execute(self, context):
		node = bpy.data.node_groups[self.m_TreeName].nodes[self.m_NodeName]
		print('operator = ', self.m_插座类型)
		#node = bpy.ops.node.add_node(type='C_Node_函数', use_transform=True)
		return {'FINISHED'}






class C_NodeSocket_动态插座(插座基类.C_Socket_基类):
	bl_label = '动态插座'
	#bl_idname = 'bl_idname_create'
	
	def __init__(self):
		super(C_NodeSocket_动态插座, self).__init__()
		#self.draw_shape = 'SQUARE'
	def draw(self, context, layout, node, text):
		#sp = layout.split(factor=0.8)
		#row = layout.row()
		layout.label(text=text+':'+self.name)
		

	def draw_color(self, context, node):
		return bl_UI配色.g_通用插座颜色










'''
class C_NodeSocket_材质集(bl_List基类.C_NodeSocket_List基类):
	bl_label = '材质集'
	bl_icon = 'MATERIAL_DATA'
	m_Value = bpy.props.CollectionProperty(type=bl_集合数据类型.C_材质数据)
	def __init__(self):
		super(C_NodeSocket_材质集, self).__init__()

class C_NodeSocket_网格(bl_List基类.C_NodeSocket_List基类):
	bl_label = '网格集'
	bl_icon = 'MATERIAL_DATA'
	m_Value = bpy.props.CollectionProperty(type=bl_集合数据类型.C_网格数据)
	def __init__(self):
		super(C_NodeSocket_网格, self).__init__()
'''







classes = (
	C_NodeSocket_物体,
	#C_插座_物体2D,
	#C_NodeSocket_图像集,
	#C_NodeSocket_纹理集,
	#C_NodeSocket_材质集,
	#C_NodeSocket_网格,


	C_Socket_标量值模板,
	C_Socket_Vector模板,


	C_NODE_AddMenu_添加插座按钮,

	C_NodeSocket_动态插座,
	
)

def register():
	for c in classes:
		bpy.utils.register_class(c)
	pass	

def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)
	pass
	

